<template>
	<view class="container">
		<view style="text-align:center;">
			<image :src="user.avatar" class="avatar" @tap="uploadAvatar"></image>
		</view>
		
		
		<form @submit="formSubmit" @reset="formReset">
			<tui-list-cell :hover="false">
				<view class="tui-line-cell">
					<view class="tui-title">昵称</view>
					<input placeholder-class="tui-phcolor" v-model="user.nickname" class="tui-input" name="nickname" placeholder="输入昵称" maxlength="50" type="text" />
				</view>
			</tui-list-cell>
			<tui-list-cell :hover="false">
				<view class="tui-line-cell">
					<view class="tui-title">性别</view>
					<view class="tui-input" @click="selectSex()" v-text="sexText()"></view>
				</view>
			</tui-list-cell>
			<tui-list-cell :hover="false">
				<view class="tui-line-cell">
					<view class="tui-title">生日</view>
					<view class="tui-input" @click="showBrithdaySelect()" v-text="birthdayText()"></view>
				</view>
			</tui-list-cell>
			<tui-list-cell :hover="false">
				<view class="tui-line-cell">
					<view class="tui-title">个性签名</view>
					<input v-model="user.description" placeholder-class="tui-phcolor" class="tui-input" name="description" placeholder="输入个性签名" maxlength="50" type="text" />
				</view>
			</tui-list-cell>
			<tui-list-cell :hover="false">
				<view class="tui-line-cell">
					<view class="tui-title">地址</view>
					<input v-model="user.address" placeholder-class="tui-phcolor" class="tui-input" name="address" placeholder="输入地址" maxlength="50" type="text" />
				</view>
			</tui-list-cell>
			<view class="tui-btn-box">
				<button class="tui-button-primary" hover-class="tui-button-hover" formType="submit" type="primary">保存</button>
			</view>
		</form>
		<tui-datetime ref="dateTime" :type="2" 
		:setDateTime="user.birthday==null?'':user.birthday"  @confirm="changeBrithday"></tui-datetime>
	</view>
</template>
<script>
	const form = require("@/components/common/tui-validation/tui-validation.js")
	import tuiDatetime from "@/components/thorui/tui-datetime/tui-datetime"
	import uploadImgUtils from "@/utils/UploadImgUtils.js"
	import userApi from "@/request/api/userApi.js"
	import userUtils from "@/utils/userUtils.js"
	export default {
		data() {
			return {
				user:{
					id:'',
					nickname:'',
					avatar:'',
					sex:null,
					birthday:'',
					description:'',
					address:''
				}
			}
		},
		created() {
			this.getUserInfo();
		},
		methods: {
			getUserInfo(){
				this.user = userApi.userBaseInfo(userUtils.getLoginUserId()).then((data)=>{
					if(data.code == 0){
						this.user = data.user
					}
				})
			},
			update(){
				userApi.update(this.user).then((data)=>{
					if(data.code == 0){
						uni.showToast({
						    title: '保存成功',
						    duration: 500,
							icon:'none'
						});
						userUtils.setLoginUserInfo(this.user) //更新cookie中的用户信息
						uni.reLaunch({url:"/pages/my/my"});
					}else{
						uni.showToast({
						    title: '保存出错',
						    duration: 500,
							icon:'none'
						});
					}
				})
			},
			formSubmit: function(e) {
				//表单规则
				let rules = [{
					name: "nickname",
					rule: ["required","minLength:2", "maxLength:10"], //可使用区间，此处主要测试功能
					msg: ["请输入昵称", "昵称必须2个或以上字符", "昵称不能超过10个字符"]
				}, {
					name: "description",
					rule: ["maxLength:30"],
					msg: ["签名不能超过30个字符"]
				},
				{
					name: "address",
					rule: ["maxLength:40"],
					msg: ["地址不能超过40个字符"]
				}];
				//进行表单检查
				let formData = e.detail.value;
				let checkRes = form.validation(formData, rules);
				if (!checkRes) { //表单通过验证
					this.update();
				} else {//表单不通过验证
					uni.showToast({
						title: checkRes,
						icon: "none"
					});
				}
			},
			//弹出生日选择组件
			showBrithdaySelect(){
				this.$refs.dateTime.show();
			},
			//生日选中后触发的方法
			changeBrithday(e){
				this.user.birthday = e.result
			},
			//生日的显示文本
			birthdayText(){
				if(this.user.birthday && this.user.birthday!= ''){
					return this.user.birthday
				}
				return "选择生日"
			},
			//弹出性别选择组件
			selectSex() {
				const arr = ['女','男']
				uni.showActionSheet({
					itemList: arr,
					success: (res) => {
						this.user.sex = res.tapIndex
					},
					fail: function(res) {
					}
				});
			},
			//性别显示的文本
			sexText(){
				if(this.user.sex == null){
					return "选择性别"
				}
				return this.user.sex==0?'女':'男';
			},
			//上传头像
			uploadAvatar(){
				uploadImgUtils.upload((imgUrl)=>{
					if(imgUrl != null){
						this.user.avatar = imgUrl;
					}
				});
			}
		}
	}
</script>

<style>
	.container {
		padding: 20rpx 0 50rpx 0;
	}

	.tui-line-cell {
		width: 100%;
		box-sizing: border-box;
		display: flex;
		align-items: center;
	}

	.tui-title {
		line-height: 32rpx;
		min-width: 120rpx;
		flex-shrink: 0;
	}

	.tui-input {
		font-size: 32rpx;
		color: #8B8B8B;
		padding-left: 20rpx;
		flex: 1;
		overflow: visible;
	}

	.tui-btn-box {
		padding: 40rpx 50rpx;
		box-sizing: border-box;
	}
	.tui-button-primary{
		background-color: #393D4E;
	}

	.tui-button-gray {
		margin-top: 30rpx;
	}

	.tui-tips {
		padding: 30rpx;
		color: #999;
		font-size: 24rpx;
	}
	
	.avatar{
	    width: 5rem;
	    height: 5rem;
	    background-color: #e5e5e5;
	    border-radius: 50%;
		margin: 20px;
	}
</style>
